package com.tsmti.tags.form;

import java.io.IOException;
import java.util.Map;
import javax.servlet.jsp.JspException;

import com.tsmti.core.common.constant.Constants;
import com.tsmti.tags.form.support.FreemarkerFormTagHelper;
import com.tsmti.tags.html.manager.HtmlComponentManager;
import com.tsmti.core.util.SpringContextUtils;
import com.tsmti.core.util.StringUtils;
import org.springframework.web.servlet.tags.form.TagWriter;


/**
 *
 * @title: TreeSelect.java
 * @package com.tsmti.tags.form
 * @description: 树形选择
 * @author:
 * @date: 2017年4月10日 下午9:16:24
 * @version
 * @copyright: 2017
 *
 */
@SuppressWarnings("serial")
public class TreeSelectTag extends HiddenInputTag {
	protected HtmlComponentManager htmlComponentManager = SpringContextUtils.getApplicationContext()
			.getBean(HtmlComponentManager.class);
	/**显示域的ID*/
	private String labelName = "";
	/**显示域的值*/
	private String labelValue = "";
	/**显示的标题*/
	private String title = "";
	/**图标*/
	private String icon = "fa-search";
	/**宽度*/
	private String layerWidth = "300px";
	/**高度*/
	private String layerHeight = "420px";
	/**是否生成,显示域及隐藏域标签*/
	private Boolean genField = Boolean.TRUE;
	/**是否多选*/
	private Boolean multiselect = Boolean.FALSE;
	/**上下级关系“p” 表示操作会影响父级节点； “s” 表示操作会影响子级节点。 */
	private String chkboxType = "ps";
	/**访问链接*/
	private String dataUrl = "";
	/**回调函数*/
	private String callback = "";

	public String getLabelName() {
		return labelName;
	}

	public void setLabelName(String labelName) {
		this.labelName = labelName;
	}

	public String getLabelValue() {
		return labelValue;
	}

	public void setLabelValue(String labelValue) {
		this.labelValue = labelValue;
	}
	@Override
	public String getTitle() {
		return title;
	}
	@Override
	public void setTitle(String title) {
		this.title = title;
	}

	public String getIcon() {
		return icon;
	}

	public void setIcon(String icon) {
		this.icon = icon;
	}

	public String getDataUrl() {
		return dataUrl;
	}

	public void setDataUrl(String dataUrl) {
		this.dataUrl = dataUrl;
	}

	public String getCallback() {
		return callback;
	}

	public void setCallback(String callback) {
		this.callback = callback;
	}

	public Boolean getGenField() {
		return genField;
	}

	public void setGenField(Boolean genField) {
		this.genField = genField;
	}

	public Boolean getMultiselect() {
		return multiselect;
	}

	public void setMultiselect(Boolean multiselect) {
		this.multiselect = multiselect;
	}

	public String getLayerWidth() {
		return layerWidth;
	}

	public void setLayerWidth(String layerWidth) {
		this.layerWidth = layerWidth;
	}

	public String getLayerHeight() {
		return layerHeight;
	}

	public void setLayerHeight(String layerHeight) {
		this.layerHeight = layerHeight;
	}

	public String getChkboxType() {
		return chkboxType;
	}

	public void setChkboxType(String chkboxType) {
		this.chkboxType = chkboxType;
	}

	@Override
	protected int writeTagContent(TagWriter tagWriter) throws JspException {
		tagWriter.startTag("input");
		writeDefaultAttributes(tagWriter);
		tagWriter.writeAttribute("type", "hidden");
		if (isDisabled()) {
			tagWriter.writeAttribute(DISABLED_ATTRIBUTE, "disabled");
		}
		String value = getDisplayString(getBoundValue(), getPropertyEditor());
		tagWriter.writeAttribute("value", processFieldValue(getName(), value, "hidden"));
		tagWriter.endTag();
		// 输出编辑器代码片段
		writeFragment();
		return SKIP_BODY;
	}

	private void writeFragment() throws JspException {
		Map<String, Object> rootMap = FreemarkerFormTagHelper.getTagStatic(this, pageContext);
		rootMap.put("name", getPath());
		String ctx = pageContext.getServletContext().getContextPath();
		String appPath = pageContext.getServletContext().getContextPath();
		String staticPath = pageContext.getServletContext().getContextPath() + "/statics";
		rootMap.put("ctx", ctx);
		rootMap.put("appPath", appPath);
		rootMap.put("staticPath", staticPath);
		String value = getDisplayString(getBoundValue(), getPropertyEditor());
		rootMap.put("value", processFieldValue(getName(), value, "hidden"));
		String fragment = htmlComponentManager.getFragmentComponent("treeselect-treeselect", rootMap);
		if (!StringUtils.isEmpty(fragment) && !Constants.NULL.equals(fragment)) {
			// 获得编辑器
			try {
				super.pageContext.getOut().write(fragment);
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

}
